<?php
namespace catchAdmin\product\request;

use catchAdmin\product\model\Product;
use catcher\base\CatchRequest;

class ProductCreateRequest extends CatchRequest
{
    protected function rules(): array
    {
        return [
            'goods_name' => [
                'require',
                function($value) {
                    $query = Product::where('goods_name', $value)
                        ->where('tenant_id', request()->user()->tenant_id)
                        ->where('deleted_at', 0);
                    
                    // 如果是更新操作，排除自身ID
                    if ($id = request()->param('id')) {
                        $query->where('id', '<>', $id);
                    }
                    
                    return $query->find() ? '已存在相同商品名称（料号）' : true;
                }
            ],
        ];
    }

    protected function message()
    {
        return [
            'goods_name.require'=>"商品名称（料号）不能为空"
        ];
    }
}